var WxNotificationCenter = require('../../../utils/WxNotificationCenter');

var that;

Page({
    onLoad: function (options) {
        that = this;
        if (options.isSwitchAddress) {
            that.setData({
                isSwitchAddress: true
            });
        }
    },
    onShow: function () {
        that.getAddress();    
    },
    add: function () {
        wx.navigateTo({
            url: '../add/add'
        });
    },
    getAddress: function () {
        wx.getStorage({
            key: 'addresses',
            success: function (res) {
                that.setData({
                    addressList: res.data || []
                });
            },
            fail: function () {
                that.setData({
                    addressList: []
                });
            }
        });
    },
    edit: function (e) {
        var index = e.currentTarget.dataset.index;
        var objectId = that.data.addressList[index].id;
        wx.navigateTo({
            url: '../add/add?objectId=' + objectId
        })
    },
    selectAddress: function (e) {
        if (!that.data.isSwitchAddress) {
            return;
        }
        var index = e.currentTarget.dataset.index;
        WxNotificationCenter.postNotificationName("addressSelectedNotification", that.data.addressList[index].id);
        wx.navigateBack();
    }
})
